#PATH=/home/suka/rv3128/cmake-3.11.2-Linux-x86_64/bin:$PATH
export ANDROID_NDK=/home/suka/legrand/android-ndk-r21b
export ANDROID_NDK_HOME=/home/suka/legrand/android-ndk-r21b
#export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_NDK:$PATH

# Build helper directory
DIR_BUILD_HELPER="$(cd `dirname $BASH_SOURCE`; pwd)"

TOP=$DIR_BUILD_HELPER/..
#code directory
SOURCE_CODE_DIR=$TOP

# Prefix directory
PREFIX_PATH=$DIR_BUILD_HELPER/android_out

# Builder work directory
WORK_PATH=$DIR_BUILD_HELPER/android_work
TOOLCHAIN_FILE=${TOP}/toolchains/toolchain-rv3128-android-armv7.cmake
#TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake

#for ffmpeg
CPU=armv7-a
ARCH=arm
HOST=arm-linux
SYSROOT=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
TOOLCHAIN=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin

#build dir
BUILD_ORDER="ffmpeg ffmpegcheck opencv opencvcheck"
#BUILD_ORDER="zlib libpng harfbuzz  freetype2 ffmpeg ffmpegcheck opencv main "
export PKG_CONFIG_SYSROOT_DIR=""
export PKG_CONFIG_LIBDIR=$PREFIX_PATH/lib/pkgconfig:$PREFIX_PATH/hare/pkgconfig
export PKG_CONFIG_PATH=$PREFIX_PATH/lib/pkgconfig:$PREFIX_PATH/hare/pkgconfig

BUILD_PAR=" -DCMAKE_ANDROID_NDK=${ANDROID_NDK} -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE}  -DCMAKE_BUILD_TYPE=Release  -DCMAKE_INSTALL_PREFIX=${PREFIX_PATH} -DCMAKE_PREFIX_PATH=${PREFIX_PATH}  -DCMAKE_NO_SYSTEM_FROM_IMPORTED=YES"


BUILD_opencv_OPTION="-DOPENCV_EXTRA_MODULES_PATH='$TOP/opencv_contrib/modules/' -DBUILD_ANDROID_EXAMPLES=OFF -DBUILD_ANDROID_PROJECTS=OFF -DBUILD_JAVA=OFF  -DWITH_FFMPEG=ON -DBUILD_SHARED_LIBS=ON -DBUILD_FAT_JAVA_LIB=OFF -DBUILD_DOCS=OFF -DOPENCV_ENABLE_PKG_CONFIG=ON -D BUILD_opencv_freetype=OFF" 
#BUILD_ffmpegcheck_OPTION="-DANDROID_ABI=armeabi-v7a -DANDROID_NATIVE_API_LEVEL=android-21 "
BUILD_libpng_OPTION="-DPNG_STATIC=OFF"
BUILD_freetype2_OPTION="-DBUILD_SHARED_LIBS=ON"
BUILD_harfbuzz_OPTION="-DBUILD_SHARED_LIBS=ON -DHB_HAVE_FREETYPE=ON"

############## make dir ###################
set -e
for var in ${BUILD_ORDER}
do
	if [ ! -d ${WORK_PATH}/${var} ];then
		mkdir -p ${WORK_PATH}/${var}
	fi
done

ffmpeg_configure()
{
	cd ${WORK_PATH}/$1
    ${SOURCE_CODE_DIR}/$1/configure \
    --prefix=$PREFIX_PATH \
	--toolchain=clang-usan \
    --enable-cross-compile \
    --target-os=android \
    --arch=$ARCH \
    --sysroot=$SYSROOT \
	--cc=$TOOLCHAIN/armv7a-linux-androideabi21-clang \
	--cxx=$TOOLCHAIN/armv7a-linux-androideabi21-clang++ \
    --strip=$TOOLCHAIN/arm-linux-androideabi-strip \
    --extra-cflags="$ADDI_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    --disable-encoders \
    --disable-decoders \
    --disable-avdevice \
    --disable-static \
    --disable-doc \
    --disable-ffplay \
    --disable-network \
    --disable-doc \
    --disable-symver \
	--disable-ffprobe \
    --disable-jni \
    --disable-libx264 \
    --enable-shared 
	return 0
}
exe_clean(){
	cd ${WORK_PATH}/$1

	make clean 
	ret=$?
	if [ ${ret} != 0 ]; then
		echo "make clean ${SOURCE_CODE_DIR}/$1 failure. errorno ${ret}"
		exit -1
	fi
}
exe_make(){
	cd ${WORK_PATH}/$1

	make -j8 && make install  
	ret=$?
	if [ ${ret} != 0 ]; then
		echo "make ${SOURCE_CODE_DIR}/$1 failure. errorno ${ret}"
		exit -1
	fi
}
exe_cmake(){
	cd ${WORK_PATH}/$1
	if [ "$1"x = "ffmpeg"x ]; then
		ffmpeg_configure $1
	else
		eval BUILD_SELF_OPTION='${'"BUILD_$1_OPTION"'}'

		echo "cmake ${SOURCE_CODE_DIR}/$1 ${BUILD_PAR} ${BUILD_OTHER_OPTION} ${BUILD_SELF_OPTION}"

		cmake ${SOURCE_CODE_DIR}/$1 ${BUILD_PAR} ${BUILD_OTHER_OPTION} ${BUILD_SELF_OPTION}
	fi
	
	ret=$?
	if [ ${ret} != 0 ]; then
		echo "cmake ${SOURCE_CODE_DIR}/$1 failure. errorno ${ret}"
		exit -1
	fi
	exe_make $1
}
help(){
	submodules_str=`echo ${BUILD_ORDER} | sed "s\ \ | \g" `
	echo "no support $var, only support parameter "
	echo "{cmake | make | clean} [$submodules_str]"
	exit -1
}
check_make(){
	if [ $1 = "YES" ]; then
		exe_make $2
		return 0
	fi
	return -1
}
check_cmake(){
	if [ $1 = "YES" ]; then
		exe_cmake $2
		return 0
	fi
	return -1
}

exe_command(){
	if [ $4x = "YES"x ];then
		exe_clean $3
		return 0
	fi
	check_cmake $1 $3
	if [ $? != 0 ]; then
		check_make $2 $3
		
		if [ $? = 0 ]; then
			return 0
		fi
		help
		return -1
	fi
	return 0
}

############## parameter ###################
CMAKE="NO"
MAKE="NO"
SINGLE_EXE="NO"
CLEAN="NO"
if [  "$*"x = x ]; then
	help
	exit -1
fi

for var in $* 
do
	if [ $var = "cmake" ]; then
		CMAKE="YES"
	elif [ $var = "make" ]; then
		MAKE="YES"
	elif [ $var = "clean" ]; then
		CLEAN="YES"
	elif [[ ${BUILD_ORDER} =~ ${var} ]]; then
		exe_command $CMAKE $MAKE $var $CLEAN
		SINGLE_EXE="YES"
	else 
	  	help
		exit -1
	fi
done





############## exe cmake ###################

if [ "$SINGLE_EXE"x != "YES"x ];then
	for var in ${BUILD_ORDER}
	do
		exe_command $CMAKE $MAKE $var $CLEAN
		 
	done
fi


